package com.example.presentshopping.ui.activity

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpActivity
import com.example.presentshopping.config.base.MyApplication
import com.example.presentshopping.config.bean.LuckydrawBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.ui.dialog.MessageDialog
import com.example.presentshopping.exent.LuckydrawEvent
import com.example.presentshopping.ui.fragment.frgmentdlog.ThankDialog
import com.example.presentshopping.ui.fragment.frgmentdlog.WinningDialog
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.tool.GsonUtils
import kotlinx.android.synthetic.main.activity_luckydraw.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe

//抽奖活动
class LuckydrawActivity : BaseMvpActivity() {
    var Luckydrawdata: List<LuckydrawBean.DataBean>? = null
    var luc: LuckydrawBean? = null
    var winning: WinningDialog? = null
    var Thank: ThankDialog? = null
    private var queryInfoPresenter: PublicPresenter? = null
    override val layoutId: Int
        get() = R.layout.activity_luckydraw

    override fun initView(savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "")
        queryInfoPresenter?.attachView(this)
        filbac.setOnClickListener { finish() }
        EventBus.getDefault().register(this)
        lottew?.setOnTransferWinningListener { posin, position ->
            winning = WinningDialog(posin)
            Thank =ThankDialog()
            Log.e("LuckydrawActivity",""+position)
            if (position==8){
                Thank?.show(supportFragmentManager ,"")
            }else{
                winning?.show(supportFragmentManager ,"")
            }

//            setDialog(posin.name)
//            text_view_scale.isClickable = true

        }
        text_view_scale?.setOnClickListener {
            startActivity(Intent(this@LuckydrawActivity,
                TurntableResActivity().javaClass))
        }
    }

    override fun onResume() {
        queryInfoPresenter?.netWorkRequestGet(HttpURL.Req_getTurntableList)
        super.onResume()
    }

    @Subscribe
    fun onEventMainThread(event: LuckydrawEvent) {
        queryInfoPresenter?.netWorkRequestGet(HttpURL.Req_getTurntableList)


    }

    fun setDialog(text: String) {
        try {

            MessageDialog.Builder(this)
                .setMessage(text)
                .setConfirm("确定")
                .setCancel("取消")
                .setListener(object : MessageDialog.OnListener {
                    override fun onConfirm(dialog: Dialog?) {

                    }

                    override fun onCancel(dialog: Dialog?) {

                    }
                })
                .show()
        } catch (e: IllegalStateException) {
        }
    }

    override fun onDestroy() {
        queryInfoPresenter?.detachView()
        Log.e("LuckydrawActivity", "onDestroy()")
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun showToast(msg: String?) {
        ToastUtil.showMsg(msg)
    }

    override fun onSuccess(data: String?) {
        val luckydrawBean = GsonUtils.getGson().fromJson(data,
            LuckydrawBean::class.java)
            tv_asunese.text = "抽奖次数：" + luckydrawBean.draw.toString()
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    fun Getbitmap(url: LuckydrawBean) {

    }

}